package com.xuetang9.t9.hotel.mapper.sql.impl;

import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.RoomInfoCondition;
import com.xuetang9.t9.hotel.mapper.sql.IMapperSql;
import org.apache.ibatis.jdbc.SQL;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Pang
 */
public class RoomInfoMapperSql implements IMapperSql {


    public String updateByStatrt(Map map){
        SQL sql = new SQL();
        List<Integer> rooms = (List<Integer>) map.get("rooms");
        Integer id = (Integer)map.get("id");
        if(rooms == null || id == null){
            return null;
        }
        sql.UPDATE("g_roominfo").SET("state_id = "+id);

        StringBuilder builder = new StringBuilder();
        builder.append("(");
        for (Integer i = 0; i < rooms.size(); i++){
            builder.append(rooms.get(i));
            if(i < rooms.size() - 1) builder.append(",");
        }
        builder.append(")");
       sql.WHERE("room_id IN"+builder.toString());
       return sql.toString();
    }

    /**
     * 条件查询
     * @param
     * @return
     */
    @Override
    public String selectByAll(Map map){
        ICondition key = (ICondition) map.get("key");
        return selectPublic("*",key,true);
    }


    /**
     * 条件查询总页数
     * @param
     * @return
     */
    @Override
    public String selectByCount(Map map){
        ICondition key = (ICondition) map.get("key");
        return selectPublic("COUNT(*)",key,false);
    }

    /**
     * 删除操作
     * @param
     * @return
     */
    public String delete(Integer... key){
        SQL sql = new SQL();
        sql.DELETE_FROM("g_roominfo");
        List<Integer> integerList = Arrays.asList(key);
        if(!isNull(integerList)){
            sql.WHERE("room_id IN"+in(integerList));
        }
        return sql.toString();
    }



    private String selectPublic(String head, ICondition key,boolean isPage){
        SQL sql = new SQL();
        sql.SELECT(head).FROM("g_roominfo");
        if(null == key){
            return sql.toString();
        }
        RoomInfoCondition roomInfoCondition = null;
        if(key instanceof RoomInfoCondition){
            roomInfoCondition = (RoomInfoCondition) key;
        }
        if(roomInfoCondition == null) {
            return sql.toString();
        }
        if(!isNull(roomInfoCondition.getFloor())){
            sql.WHERE("floor_id IN"+in(roomInfoCondition.getFloor()));
        }
        if(!isNull(roomInfoCondition.getState())){
            sql.WHERE("state_id IN"+in(roomInfoCondition.getState()));
        }
        if(!isNull(roomInfoCondition.getType())){
            sql.WHERE("type_id IN"+in(roomInfoCondition.getType()));
        }
        if(roomInfoCondition.getRoomName() != null && !roomInfoCondition.getRoomName().isEmpty()){
            sql.WHERE("room_num LIKE '%"+roomInfoCondition.getRoomName()+"%'");
        }
        if(roomInfoCondition.judge()  && isPage){
            sql.LIMIT(roomInfoCondition.getCurrPage()+","+roomInfoCondition.getPageSize());
        }
        return sql.toString();
    }

    private String in(List<Integer> ids){
        StringBuilder builder = new StringBuilder();
        builder.append("(");
        for (Integer i = 0; i < ids.size(); i++){
            builder.append(ids.get(i));
            if(i < ids.size() - 1) builder.append(",");
        }
        builder.append(")");
        return builder.toString();
    }

    private boolean isNull(List<Integer> ids){
        if(ids == null || ids.size() == 0) {return true;};
        return false;
    }
}
